from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.Qt import QMainWindow, QApplication, QIcon, QWidget, QDesktopWidget, QLabel, QMessageBox
from config import Cfg
from utils.add_btn_css import AddBtnCss
from utils.new_window import NewWindow
from page import PageWin
import images_qrc


class IndexWin(QMainWindow):
    def __init__(self):
        super(IndexWin, self).__init__()

        desktop = QApplication.desktop()
        screen_rect = desktop.screenGeometry()
        height = screen_rect.height()
        width = screen_rect.width()

        self.h_ratio = height / Cfg.dic['winh']
        self.w_ratio = width / Cfg.dic['winw']

        self.setWindowIcon(QIcon(':images/logo.png'))

    def setup_ui(self, mainwin):
        self.window = mainwin
        self.window.resize(int(800 * self.w_ratio), int(600 * self.h_ratio))
        self.put_center(self.window)

        # 背景图片
        window_pale = QtGui.QPalette()
        window_pale.setBrush(
            self.backgroundRole(),
            QtGui.QBrush(
                QtGui.QPixmap(':images/index.png').scaled(
                    int(1100 * self.w_ratio),
                    int(660 * self.h_ratio)
                )
            )
        )
        self.window.setPalette(window_pale)

        # 声明一个插板
        self.widget = QtWidgets.QWidget(self.window)
        self.widget.resize(int(800 * self.w_ratio), int(600 * self.h_ratio))

        # 几个按钮
        self.launch_btn = AddBtnCss('launch', self.launch, self.widget)
        self.launch_btn.setGeometry(
            QtCore.QRect(
                int(self.w_ratio * 600),
                int(self.h_ratio * 30),
                int(self.w_ratio * 200),
                int(self.h_ratio * 200)
            )
        )
        self.launch_btn.setObjectName('登录系统')

        # 最小化
        self.mini_btn = QtWidgets.QPushButton(self.widget)
        self.mini_btn.setObjectName('最小化')
        self.mini_btn.setGeometry(
            QtCore.QRect(
                int(self.w_ratio * 720),
                0,
                int(self.w_ratio * 30),
                int(self.h_ratio * 30)
            )
        )
        self.mini_btn.setStyleSheet(
            'QPushButton{border-image:url(:images/mini.png);};'
            'QPushButton:hover{border-image:url(:images/mini_1.png);};'
        )
        self.mini_btn.clicked.connect(self._mini)

        # 关闭
        self.out_btn = QtWidgets.QPushButton(self.widget)
        self.out_btn.setObjectName('退出')
        self.out_btn.setGeometry(
            QtCore.QRect(
                int(self.w_ratio * 760),
                0,
                int(self.w_ratio * 30),
                int(self.h_ratio * 30)
            )
        )
        self.out_btn.setStyleSheet(
            'QPushButton{border-image: url(:images/out.png);};'
            'QPushButton:hover{border-image: url(:images/out_1.png);};'
        )
        self.out_btn.clicked.connect(self._out)

    def retranslate_ui(self, Dialog):
        _translate = QtCore.QCoreApplication.translate

    def put_center(self, window):
        screen = QDesktopWidget().screenGeometry()
        size = window.geometry()
        window.move(
            (screen.width() - size.width()) / 2,
            (screen.height() - size.height()) / 3
        )

    def _out(self):
        self.window.close()

    def _mini(self):
        self.window.showMinimized()

    def launch(self):
        # 进入page
        win = NewWindow()
        win.setWindowIcon(QIcon(':images/logo.png'))

        pagewin = Cfg.dic['__pagewin__']
        pagewin.setup_ui(win)

        win.setLoc(
            Cfg.dic['__indexwin__'].x(),
            Cfg.dic['__indexwin__'].y()
        )
        win.show()

        Cfg.dic['__indexwin__'].close()
        Cfg.dic['__indexwin__'] = win


if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    win = IndexWin()
    win.setup_ui(win)
    win.show()
    app.exit(app.exec_())
